package cn.tedu.aiumall.user.webapi.sendSms;



import cn.tedu.aiumall.common.pojo.user.dto.UserAddNewDTO;
import cn.tedu.aiumall.common.pojo.user.dto.UserLoginDTO;
import cn.tedu.aiumall.common.pojo.user.vo.UserLoginInfoVO;
import cn.tedu.aiumall.user.webapi.ex.ServiceCode;
import cn.tedu.aiumall.user.webapi.ex.ServiceException;
import cn.tedu.aiumall.user.webapi.mapper.UserMapper;
import cn.tedu.aiumall.user.webapi.service.impl.UserServiceImpl;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Slf4j
@Service
public class SendMessageImpl implements ISendMessageService {

    @Autowired
    UserMapper userMapper;

    @Override
    public String sendMessage(UserLoginDTO userLoginDTO) {
        String usernameOrPhoneOrEmail=userLoginDTO.getUsernameOrPhoneOrEmail();
        UserLoginInfoVO queryResult =
                userMapper.getStandardByUsernameOrPhoneOrEmail(
                        usernameOrPhoneOrEmail);
        if (queryResult==null){
            String message="登陆失败！该账户不存在！";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if (queryResult.getIsEnable()!=1){
            String message="登陆失败，该账户已禁用!";
            log.info(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        String phone= queryResult.getPhone();
        //阿里云短信发送代码
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai", "LTAI5tMHsQ6x2Jm3wCk66Gmm",
                "u7lpy5TdInwqMtVTSfnL5c7EojXSLB");

        IAcsClient client = new DefaultAcsClient(profile);
        Random random=new Random();
        int i=random.nextInt(8999)+1000;
        String sendMessage=i+"";
        UserServiceImpl.sendMessage =sendMessage;

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(phone);
        request.setTemplateParam("{\"code\":\""+ sendMessage +"\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));

        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return sendMessage;
    }

    @Override
    public String addNewMessage(UserAddNewDTO addNewDTO) {
        String phone = addNewDTO.getPhone();
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai", "LTAI5tMHsQ6x2Jm3wCk66Gmm",
                "u7lpy5TdInwqMtVTSfnL5c7EojXSLB");

        IAcsClient client = new DefaultAcsClient(profile);
        Random random=new Random();
        int i=random.nextInt(8999)+1000;
        String sendMessage=i+"";
        UserServiceImpl.sendMessage =sendMessage;

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(phone);
        request.setTemplateParam("{\"code\":\""+ sendMessage +"\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));

        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return sendMessage;
    }
}
